#!/usr/bin/env bash
set -e

echo "设定远程仓库地址..."
if[ ! -d "/root/.ssh"];then
if [[ ${ID_RSA} ]];then
echo "设置私钥，以更新私有代码"
mkdir -p /root/.ssh  && echo -e ${ID_RSA} > /root/.ssh/id_rsa  && chmod 600 /root/.ssh/id_rsa  && ssh-keyscan gitee.com > /root/.ssh/known_hosts
fi
fi
cd /scripts
git clone --no-checkout -b ${REPO_BRANCH} ${REPO_URL} /unicomTemp
mv -f /unicomTemp/.git /scripts
rm -rf /unicomTemp
git reset --hard HEAD
echo "git pull拉取最新代码..."
cd  /scripts && git fetch --all && git reset --hard origin/${REPO_BRANCH}
echo "npm install 安装最新依赖"
npm config set registry https://registry.npm.taobao.org 
npm install -s --prefix /scripts
echo "------------------------设置账号信息进入环境变量----------------------------------"
echo "" > /scripts/config/.env
echo "UNICOM_USERNAME='${UNICOM_USERNAME}'" >> /scripts/config/.env
echo "UNICOM_PASSWORD='${UNICOM_PASSWORD}'" >> /scripts/config/.env
echo "UNICOM_APPID='${UNICOM_APPID}'" >> /scripts/config/.env

echo "------------------------------------------------执行定时任务任务shell脚本------------------------------------------------"

# 移除定时任务
crontab -r
crontab ${BASE_DIR}/unicom/default_crontab_list.md
/usr/sbin/crond -c /etc/crontabs -f
